//
//  THWebSheetView.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "THWebSheetView.h"
#import "THWKWebView.h"
#import "THSlidePopupView.h"
#import "UIViewController+THCurretnVC.h"
#import "THCurrentVCWindowsManager.h"

@interface THWebSheetView()

@property (nonatomic, strong) THSlidePopupView *dragView;

@property (nonatomic, strong) UIView *bgV;



@property (nonatomic, strong) THWKWebView *webView;

@property (nonatomic, strong) UIView *topLine;

@end

@implementation THWebSheetView

+ (instancetype)getInstance
{
    return [self getInstanceSize:CGSizeZero];
}

+ (instancetype)getInstanceSize:(CGSize)size
{
    CGFloat height = kMainScreenHeight - kSizeRateTo(140);
    if (size.width > 0 && size.height > 0) {
        height = [UIScreen mainScreen].bounds.size.width * size.height/size.width;
    }
    THWebSheetView *sheet = [[THWebSheetView alloc] initWithFrame:CGRectMake(0, 0, kMainScreenWidth,  MIN(height, [UIScreen mainScreen].bounds.size.height))];
    return sheet;
}

+ (instancetype)getInstanceUrl:(NSString *)url
{
    CGFloat w = 0;
    CGFloat h = 0;
    NSDictionary*dict = [NSString paramDictFromURL:url];
    w = [dict[@"w"] floatValue];
    h = [dict[@"h"] floatValue];
    
    if (w <= 20 || h <= 20) {
        w = 0;
        h = 0;
    }
    return [self getInstanceSize:CGSizeMake(w, h)];
}

/**子类添加控件 */
-(void)setupUI
{
    self.backgroundColor = [UIColor whiteColor];

    _bgV = UIView.new;
    self.bgV.backgroundColor = [UIColor whiteColor];
//    self.bgV.layer.cornerRadius = kSizeRateTo(8);
    [self addSubview:self.bgV];
    [self.bgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(0);
    }];
    
    [self.bgV addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.bgV);
    }];
    
    UIView *topLine = [UIView new];
    topLine.backgroundColor = ColorFromHex(0xC8C8C8);
    [self addSubview:topLine];
    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(0);
        make.top.equalTo(kSizeRateTo(6));
        make.size.equalTo(CGSizeMake(kSizeRateTo(81), kSizeRateTo(4)));
    }];
    
    topLine.layer.cornerRadius = kSizeRateTo(2);
    topLine.layer.masksToBounds = YES;
    _topLine = topLine;
}

- (void)configureWithEntity:(THBaseEntity *)entity
{

}

- (void)showWithUrl:(NSString *)url
{
    [self showWithUrl:url inView:[THCurrentVCWindowsManager shareManager].currentWindow];
}

- (void)viewWillDisMiss
{
    [self.webView evaluateJavaScript:@"_oh_i_m_dead_()" completionHandler:^(id _Nullable item, NSError * _Nullable error) {
        
    }];
}

- (void)showWithUrl:(NSString *)url inView:(UIView *)view
{
    if (url.length <= 0) {
        return;
    }
    [view endEditing:YES];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url appendTimeUrl].kURL];

    [self.webView loadRequest:request];
    _dragView = [THSlidePopupView popupViewWithFrame:view.bounds contentView:self];
    if ([url containsString:@"game=T"]) {
        _dragView.panGesture.enabled = NO;
        
        view = [UIViewController currentViewController].view;
        
    }
    
    if ([url containsString:@"bg=F"]) {
        self.backgroundColor = [UIColor clearColor];
        _bgV.backgroundColor = [UIColor clearColor];
        _webView.backgroundColor = [UIColor clearColor];
        _topLine.hidden = YES;
    }
    
    [_dragView setHeaderCornerRadius:kSizeRateTo(12)];

    [_dragView showFrom:view completion:^{
    }];
    @weakify(self);
    [_dragView setViewWillDisMissBlock:^BOOL{
        @strongify(self);
        [self requestUserClose];
        return NO;
    }];
}

- (void)navShowWithUrl:(NSString *)url
                 inView:(UIView *)view
                 title:(NSString *)title
{
    if (url.length <= 0) {
        return;
    }
    [view endEditing:YES];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[url appendTimeUrl]]];
    [self.webView loadRequest:request];
    
    UIView *topBg = UIView.new;
    topBg.backgroundColor = ColorFromHex(0xEDEDED);
    [self.bgV addSubview:topBg];
    [topBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(0);
        make.height.equalTo(55);
    }];
    
    UILabel *titleLab = UILabel.new;
    titleLab.font = kFontMedium(16);
    titleLab.textColor = Color_text_33;
    titleLab.text = title;
    [topBg addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(0);
        make.height.equalTo(40);
        make.centerX.equalTo(0);
    }];
    
    UIButton *backBtn = UIButton.new;
    [backBtn setImage:[UIImage imageNamed:@"baseImg_web_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(dismis) forControlEvents:UIControlEventTouchUpInside];
    [topBg addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(15);
        make.size.equalTo(24);
        make.bottom.equalTo(-8);
    }];
    
    
    [self.webView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(0);
        make.top.equalTo(55);
    }];
    
    _dragView = [THSlidePopupView popupViewWithFrame:view.bounds contentView:self];

    [_dragView setHeaderCornerRadius:kSizeRateTo(12)];
    [_dragView showFrom:view completion:^{
    }];

}

- (void)requestUserClose
{
    if ([self.webView.request.URL.absoluteString containsString:@"game=T"]) {
        [[[THAlertView alloc] initWithTitle:@"温馨提示"
                                       desc:@"关闭游戏将结束本局，确认关闭吗？"
                               sureBtnTitle:@"继续玩"
                             cancelBtnTitle:@"关闭"
                              actionHandler:^(THAlertView * _Nonnull alertView) {
        } cancelHandler:^(THAlertView * _Nonnull alertView) {
            [self dismis];
        }] show] ;
    }else{
        [self dismis];
    }
}

- (void)dismis
{
    [self viewWillDisMiss];
    [self.dragView closeWithoutRequest:^{
        
    }];
}


- (THWKWebView *)webView
{
    if (!_webView) {
        _webView = [THWKWebView getInstance];
        _webView.clipsToBounds = YES;
//        _webView.delegate = self;
        _webView.opaque = NO;
//        _webView.scrollView.mj_header = self.mj_header;
        _webView.tag = THContentViewScrollViewTag;
    }
    return _webView;
}


@end
